import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import '../plug/wlist.dart';
import '../plug/Carousel.dart';
import 'banner.dart';
import 'package:hairen_app/server/server.dart';
import 'package:hairen_app/pages/player/player.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:hairen_app/pages/plug/func.dart';


class BodyPage extends StatefulWidget {
  final int tid;

  BodyPage({Key key, this.tid}) : super(key: key);
  @override
  State<StatefulWidget> createState() => new BodyPageState();
}

class BodyPageState extends State<BodyPage> {

  ScrollController _scrollcontrolle=new ScrollController();
  
  double top=0.0;
  @override
  initState() {
    super.initState();
    _scrollcontrolle.addListener((){
      print(_scrollcontrolle.offset);
      setState(() {
        if(_scrollcontrolle.offset<0){
          top=_scrollcontrolle.offset.abs();
        }

      });
    });
  }
  isType() {
      if (widget.tid == 1) {
        return Server.getUpTime();
      }
      if (widget.tid == 2) {
        return Server.getHot();
      }
      if (widget.tid == 3) {
        return Server.getRecommend();
      }
    }
    @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
//    Server.getFavorite().then((ret){
//      print(ret);
//    });
    return new Stack(
      children: <Widget>[
        top>20.0?new Positioned(
              top:top-10.0>200?200.0:top-10.0,
              left: 0.0,
              right: 0.0,
              child:new Align(child: Text('不要对我拉拉扯扯'),),
          ):new Text(''),
          new ListView(
          controller: _scrollcontrolle,
            physics: BouncingScrollPhysics(),
            padding: EdgeInsets.only(top: 0.0),
            children: <Widget>[
              new Container(
                margin:
                EdgeInsets.only(top: 15.0, left: 15.0, right: 15.0, bottom: 8.0),
                height: 200.0,
                child: new BannerPage(),
              ),
              new Divider(),
              new Container(
                child: FutureBuilder(
                  future: isType(),
                  builder: (context, res) {
                    if (res.hasData) {
                      List<Widget> list = <Widget>[];
                      var data = res.data['data']['data'];
                      for (var i = 0; i < data.length; i++) {
                        list.add(new Expanded(
                            flex: 1,
                            child: Container(
                                margin: EdgeInsets.only(
                                    left: 7.5, right: 7.5, bottom: 7.5, top: 7.5),
                                child: new InkWell(
                                  child: new Column(
                                    children: <Widget>[
                                      new Container(
                                        height: 200.0,
                                        child: new Stack(
                                          children: <Widget>[
                                            new ConstrainedBox(
                                              constraints: new BoxConstraints.expand(),
                                              child:  new Image.network(

                                                  data[i]['logo'],
                                                  fit: BoxFit.cover
                                              ),),
                                            new Container(
                                              margin: EdgeInsets.only(top: 170.0),
                                              height: 30.0,
                                              child: new Material(
                                                color:
                                                Color.fromRGBO(14, 14, 14, 0.51),
                                                child: new Row(children: <Widget>[
                                                  new Expanded(
                                                    flex: 1,
                                                    child: new Container(
                                                      padding:
                                                      EdgeInsets.only(left: 10.0),
                                                      child: new Row(
                                                        children: <Widget>[
                                                          new Icon(
                                                            Icons.category,
                                                            color: Colors.white,
                                                            size: 12.0,
                                                          ),
                                                          new Container(
                                                            width: 5.0,
                                                          ),
                                                          new Text(
                                                            data[i]['type'],
                                                            style: new TextStyle(
                                                                color: Colors.white,
                                                                fontSize: 12.0),
                                                          )
                                                        ],
                                                      ),
                                                    ),
                                                  ),
                                                  new Expanded(
                                                    flex: 1,
                                                    child: new Container(
                                                        padding: EdgeInsets.only(
                                                            right: 10.0),
                                                        child: new Row(
                                                          mainAxisAlignment:
                                                          MainAxisAlignment.end,
                                                          children: <Widget>[
                                                            new Icon(
                                                              Icons.access_time,
                                                              color: Colors.white,
                                                              size: 12.0,
                                                            ),
                                                            new Container(
                                                              width: 5.0,
                                                            ),
                                                            new Text(
                                                              func.diffTime(DateTime.parse(data[i]['update']), DateTime.now()),
                                                              style: new TextStyle(
                                                                  color: Colors.white,
                                                                  fontSize: 12.0),
                                                            )
                                                          ],
                                                        )),
                                                  ),
                                                ]),
                                                borderRadius: BorderRadius.only(
                                                    bottomLeft: Radius.circular(8.0),
                                                    bottomRight: Radius.circular(8.0),
                                                    topLeft: Radius.circular(0.0),
                                                    topRight: Radius.circular(0.0)),
                                              ),
                                            ),
                                          ],
                                        ),
                                        decoration: new BoxDecoration(
      //                  border: Border.all(
      //                      width: 1.0, color: Color.fromRGBO(239, 239, 239, 1.0)),
                                          color: Colors.blue,
                                          borderRadius:
                                          BorderRadius.all(Radius.circular(8.0)),
      //                  image: new DecorationImage(
      //                    image: new NetworkImage(
      //                        'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=4097200241,3195669789&fm=26&gp=0.jpg'),
      //                    fit: BoxFit.cover,
      //                  ),
                                        ),
                                      ),
                                      new Container(
                                        height: 40.0,
                                        child: new ListTile(
                                          title: new Text(
                                            data[i]['title'],
                                            maxLines: 1,
      //                    textAlign: TextAlign.center,
                                            style: new TextStyle(fontSize: 16.0),
                                          ),
      //                  subtitle: new Text('收账不成，无处可归'),
                                        ),
                                      )
                                    ],
                                  ),
                                  onTap: () {
                                    Navigator.of(context).push(new CupertinoPageRoute(

                                        builder: (context) => new PlayerPage(
                                            mid: int.parse(data[i]['id']))));
                                  },
                                ))));
                      }
                      return new WlistPage(
                        children: list,
                        column: 2,
                      );
                    } else {
                      return new Container(
                        height: 100.0,
                        child: new SpinKitWave(
                          color: Color.fromRGBO(251, 114, 153, 1.0),
                          size: 30.0,
                        ),
                      );
                    }
                  },
                ),
              )
            ]
          ),
      ],
    );
  }
}


//)
